Release 10.1A: OpenEdge Development:
Progress 4GL Handbook
The CASE statement
If you have a sequence of
IF,THEN,ELSEclauses that all operate on different values of the same variable or expression, you can combine these into a single block using theCASEstatement, which has this syntax:
The
expressioncan be a simple field or variable or any other expression involving multiple fields or values. Part of the optimization of theCASEstatement is that it evaluates the expression only once, when theCASEstatement is entered. By contrast, nestedIFstatements evaluate the expression in eachIFclause, even if the expression is the same each time.Following the block header are a number of
WHENclauses, each of which starts with avaluefor theexpression, followed byTHEN, followed by a statement or block to execute if the expression has that value. You can combine multipleWHENclauses withORif the same block or statement executes on multiple values of the expression.Finally, the
CASEblock can conclude with an optionalOTHERWISEclause with a statement or block to execute if the expression matches none of the values in theWHENclauses.The
CASEstatement is most useful when a variable or field can have one of a small number of possible values, and the procedure needs to react differently to each value.Here’s a simple example that uses the
CASEstatement to count the number of Orders of different types. There are four valid values for the OrderStatus field. TheOTHERWISEclause tallies any that don’t match any of the valid values (as it turns out, there aren’t any):
Figure 21–1 shows the result.
Figure 21–1: Result of CASE statement example
![]()
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |